package com.youdoneed.community.forum.service;

import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.MsgviewrecordMapper;
import com.youdoneed.community.forum.entity.Msgviewrecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by lid on 2017/11/07 0005.
 * 3D视频、酷视频浏览记录服务接口
 */
@Service
public class MsgViewRecordService {
    private final Logger logger = LoggerFactory.getLogger(MsgViewRecordService.class);

    @Autowired
    MsgviewrecordMapper msgviewrecordMapper;

    /**
     * 3D视频、酷视频浏览记录
     *
     * @param msgId
     * @param userId
     * @param msgType
     * @return int
     * @author lid
     * @date 2017.11.7
     */
    @Transactional
    public int saveRecord(String msgId, String userId, String msgType) {
        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId为空！");
        }

        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空！");
        }

        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空！");
        }

        try {
            Msgviewrecord msgviewrecord = new Msgviewrecord();
            String recordId = StringUUIdUtil.getUUID();
            Date createDate = new Date();

            msgviewrecord.setCreateDate(createDate);
            msgviewrecord.setForumMsgId(msgId);
            msgviewrecord.setUserId(userId);
            msgviewrecord.setViewRecordId(recordId);
            msgviewrecord.setMsgType(msgType);

            return msgviewrecordMapper.insert(msgviewrecord);

        } catch (Exception e) {
            throw new AppException("保存浏览记录失败！");
        }
    }

    /**
     * 获取用户的观看记录
     * @param userId
     * @return
     */
    public List<String> getRecordsByUserId(String userId){
        if(!StringUtils.hasText(userId)){
            throw new AppException("userId为空!");
        }

        try {
            return msgviewrecordMapper.getRecordsByUserId(userId);
        }catch (Exception e){
            logger.info(e.getMessage());
            throw new AppException("获取用户的观看记录失败！");
        }
    }
}
